<?php

namespace App\Http\Controllers\UserApi;

use App\Services\Admin\SystemSet;
use Illuminate\Http\Request;
use App\Services\Api\CouponServices;
use App\Services\Api\UserServices;
use App\Http\Requests\UserApi\CouponRequest;

class CouponController extends BaseController
{
    public function coupon_list(Request $request){
        $param = $request->input();
        $param['user_id'] = auth('api') -> user()->id;
        $data = CouponServices::coupon_list($param);
        return self::success($data);
    }

    public function coupon_info(CouponRequest $request){
        $data = CouponServices::coupon_info($request->coupon_id,auth('api') -> user()->id);
        return self::success($data);
    }

    public function add_user_coupon(CouponRequest $request){
        $param = $request->post();
        $param['user_id'] = auth('api') -> user()->id;
        $res = CouponServices::add_user_coupon($param);

        if($res['status'] == true){
            return self::success();
        }
        return self::fail($res['msg']);
    }

    public function my_user_coupon(Request $request, UserServices $services){
        $param = $request->input();

        if(empty($param['type']))$param['type']=1;

        $param['user_id'] = auth('api') -> user()->id;

        switch ($param['type']){
            case '1':
                $result = CouponServices::my_user_coupon($param)->paginate(15);
                break;
            case '2':
                $result = $services->coupon();
                break;
        }

        $top_image = SystemSet::system_image(4);

        $custom = collect(['top_image' => $top_image]);

        $result = $custom->merge($result);

        return self::success($result);
    }

    public function my_user_coupon_info(UserServices $services)
    {
        $input = \request()->all();

        if(empty($input['type']))$input['type']=1;

        try {
            switch ($input['type']){
                case '1':
                    $result = CouponServices::my_user_coupon_info();
                    break;
                case '2':
                    $result = $services->coupon_info();
                    break;
            }
        } catch (\Exception $e){
            return self::fail($e->getMessage());
        }

        return self::success($result);
    }

}
